package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.OrdersDao;
import com.itheima.domain.store.Orders;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.OrdersService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public List<Orders> findAll() {
        SqlSession sqlSession = null;
        List<Orders> list= null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            list = mapper.findAll();

        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public Orders findById(String id) {
        SqlSession sqlSession = null;
        Orders order = null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            order = mapper.findById(id);


        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return order;
    }

    @Override
    public void addOrders(Orders orders) {
        SqlSession sqlSession = null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            mapper.save(orders);


        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void deleteOrders(String id) {
        SqlSession sqlSession = null;

        //try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            if(id.contains(",")){
                String[] split = id.split(",");
                for (String s : split) {
                    mapper.delete(id);
                }
            }else{
                mapper.delete(id);
            }
//        } catch (Exception e) {
//            System.out.println("回滚了1");
//            TransactionUtil.rollback(sqlSession);
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }

    }

    @Override
    public void updateOrders(Orders orders) {

        SqlSession sqlSession = null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            mapper.update(orders);

        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public PageInfo findByPage(int currentPage, int pageSize) {

//        开启分页

        SqlSession sqlSession = null;
        PageInfo pageInfo = null;


       //try {
            sqlSession =  MapperFactory.getSqlSession();
            OrdersDao mapper = MapperFactory.getMapper(sqlSession, OrdersDao.class);

            //PageHelper.startPage(1,5);
            PageHelper.startPage(currentPage,pageSize);
            List<Orders> all = mapper.findAll();
            System.out.println(all);
//            使用分页插件进行分页处理
            pageInfo = new PageInfo(all);




//        } catch (Exception e) {
//            System.out.println("回滚了");
//            TransactionUtil.rollback(sqlSession);
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }

        return pageInfo;
    }
}
